package com.yzt.zhuju.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.yzt.zhuju.RXingUtils;

/**
 * 功能：缓存Im语音的消息id
 */
public class VoiceMsgIdHelper extends SQLiteOpenHelper {
    /**
     * 数据库名
     */
    private static final String DB_NAME = "_history_voice_msg_1.db";
    private static final int DB_VERSION = 2;

    private static SQLiteDatabase sqliteDB;
    private static VoiceMsgIdHelper mVoiceMsgIdHelper;

    public static VoiceMsgIdHelper getInstance(Context context) {
        if (mVoiceMsgIdHelper == null) {
            mVoiceMsgIdHelper = new VoiceMsgIdHelper(context);
        }
        return mVoiceMsgIdHelper;
    }


    String DB_TABLE_VOICERHISTORY_NAME = "voice_msg_search";

    //字段
    String DB_KEY_ID = "_id";
    String DB_KEY_NAME = "msg_id";


    String CREATE_TBL_MOODDIARY_INFO = " CREATE TABLE IF NOT EXISTS "//
            + DB_TABLE_VOICERHISTORY_NAME + "(" + DB_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " //
            + DB_KEY_NAME + " TEXT ) ";


    public VoiceMsgIdHelper(Context context) {
        super(context, RXingUtils.getUserId() + DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TBL_MOODDIARY_INFO);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(" DROP TABLE IF EXISTS " + DB_TABLE_VOICERHISTORY_NAME);
    }

    /**
     *
     */
    public void insertVoicerHistory(String key) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();//开启事务
        Cursor cursor = null;
        try {
            cursor = db.query(DB_TABLE_VOICERHISTORY_NAME, null, DB_KEY_NAME + "=? ", new String[]{key}, null, null, null);
            if (cursor.moveToFirst()) { // 存在
                ContentValues contentValues = ContentValues(key);
                db.update(DB_TABLE_VOICERHISTORY_NAME, contentValues, DB_KEY_NAME + "=?", new String[]{key});
            } else {
                ContentValues contentValues = ContentValues(key);
                db.insert(DB_TABLE_VOICERHISTORY_NAME, null, contentValues);
            }
            db.setTransactionSuccessful();//设置事务的标志为True
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();//结束事务,有两种情况：commit,rollback,
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
    }

    /**
     *
     */
    public boolean has(String key) {
        SQLiteDatabase db = this.getWritableDatabase();
        try {
            Cursor cursor = db.query(DB_TABLE_VOICERHISTORY_NAME, null, DB_KEY_NAME + "=? ", new String[]{key}, null, null, null);
            return cursor.moveToFirst();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     *
     */
    public void deleteTable() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();//开启事务
        db.execSQL("DELETE FROM " + DB_TABLE_VOICERHISTORY_NAME);
    }


    public ContentValues ContentValues(String key) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DB_KEY_NAME, key);
        return contentValues;
    }
}
